package com.android.kotlincoroutinemvvmdemo.view
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import com.android.kotlincoroutinemvvmdemo.R

/*
* FriendCircleView
* */
class FriendCircleView @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : BaseView(context, attributeSet) {

    private val mPaint0 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            //strokeCap = Paint.Cap.ROUND
            style = Paint.Style.STROKE
            color = context.resources.getColor(R.color.color_EA992E)
        }
    }

    private val mPaint1 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            //strokeCap = Paint.Cap.ROUND
            style = Paint.Style.STROKE
            color = context.resources.getColor(R.color.color_A4F85D)
        }
    }

    private val mPaint2 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            //strokeCap = Paint.Cap.ROUND
            style = Paint.Style.STROKE
            color = context.resources.getColor(R.color.color_BAED07)
        }
    }

    private val mPaint3 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            //strokeCap = Paint.Cap.ROUND
            style = Paint.Style.STROKE
            color = context.resources.getColor(R.color.color_00796B)
        }
    }

    private val mPaint4 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            //strokeCap = Paint.Cap.ROUND
            style = Paint.Style.STROKE
            color = context.resources.getColor(R.color.color_388E3C)
        }
    }

    private val mPaint5 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            //strokeCap = Paint.Cap.ROUND
            style = Paint.Style.STROKE
            color = context.resources.getColor(R.color.color_689F38)
        }
    }

    private val mPaint6 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            //strokeCap = Paint.Cap.ROUND
            style = Paint.Style.STROKE
            color = context.resources.getColor(R.color.color_FF8A80)
        }
    }

    private val mPaint7 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            //strokeCap = Paint.Cap.ROUND
            style = Paint.Style.STROKE
            color = context.resources.getColor(R.color.color_FF80AB)
        }
    }

    private val mPainta0 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            //strokeCap = Paint.Cap.ROUND
            style = Paint.Style.STROKE
            color = context.resources.getColor(R.color.color_A98DE8)
        }
    }

    private val mPainta1 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            //strokeCap = Paint.Cap.ROUND
            style = Paint.Style.STROKE
            color = context.resources.getColor(R.color.color_E88DBF)
        }
    }

    private val mPainta2 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            //strokeCap = Paint.Cap.ROUND
            style = Paint.Style.STROKE
            color = context.resources.getColor(R.color.color_CF5C9B)
        }
    }

    private val mPaintb0 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            //strokeCap = Paint.Cap.ROUND
            style = Paint.Style.STROKE
            color = context.resources.getColor(R.color.color_E85F90)
        }
    }

    private val mPaintArr =
        arrayOf(mPaint0, mPaint1, mPaint2, mPaint3, mPaint4, mPaint5, mPaint6, mPaint7)

    private val mPaintArr0 =
        arrayOf(mPaintb0, mPainta0, mPainta1, mPainta2)

    private val mPaintArr1 =
        arrayOf(mPaintb0, mPainta0, mPainta1, mPainta2)

    private val mPaintArr2 =
        arrayOf(mPaintb0, mPainta0, mPainta2, mPainta1)

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        setBackgroundColor(context.resources.getColor(R.color.color_F3F3F3))
        canvas?.apply {
            val cx = mWidth.toFloat() / 2
            val cy = mHeight.toFloat() / 2
            save()
            translate(cx, cy)

            val mStrokeWidth = (cx / 20).coerceAtLeast(minStrokeWidth)

            for (i in 0..7) {
                mPaintArr[i].strokeWidth = mStrokeWidth
                drawArc(
                    -cx * 5 / 6,
                    -cy * 5 / 6,
                    cx * 5 / 6,
                    cy * 5 / 6,
                    280F + i * 45,
                    45F,
                    false,
                    mPaintArr[i]
                )
            }

            for (i in 0..3) {
                drawArc(
                    -cx * 1 / 6,
                    -cy * 1 / 6,
                    cx * 1 / 6,
                    cy * 1 / 6,
                    45F + i * 90F,
                    60F,
                    false,
                    mPaintArr2[i]
                )
            }

            save()
            translate(0F, cy * 0 / 6)
            for (i in 0..3) {
                mPaintArr0[i].strokeWidth = mStrokeWidth
                rotate(i * 90.0F)
                drawLine(
                    cx * 1 / 12,
                    cy * 1 / 6,
                    cx * 5 / 6 - mStrokeWidth / 2,
                    cy * 1 / 6 - mStrokeWidth / 2,
                    mPaintArr0[i]
                )
            }
            restore()

            save()
            for (i in 0..3) {
                mPaintArr1[i].strokeWidth = mStrokeWidth
                rotate(i * 90.0F)
                drawLine(
                    cx * 5 / 6 * Math.cos(Math.toRadians(310.0)).toFloat() + cx / 6 - mStrokeWidth / 2,
                    cy * 5 / 6 * Math.sin(Math.toRadians(310.0)).toFloat() + cx / 6 - mStrokeWidth / 2,
                    cx * 1 / 12,
                    cy * 1 / 6,
                    mPaintArr1[i]
                )
            }
            restore()

            restore()
        }
    }
}